// src/stores/useMaterial.ts
import { makeAutoObservable } from 'mobx';
import { defaultStatusMap } from '@/config/defaultStatus/defaultStatusMap';
import { ComsTypeEnum, type Material, type Status } from '@/types';
import {
  addOption,
  rmOption,
  setCurrentStatus,
  setPicLinkByIndex,
  setTextStatus,
  setValueByIndex,
  updateInitStatusBeforeAdd
} from './actions';

// 哪些业务组件要初始化
const initKey: Material[] = [ComsTypeEnum.personInfoGender, ComsTypeEnum.personInfoEdu];
const initState: Record<Material, Status> = {};
initKey.forEach(key => {
  const defaultComStatus = defaultStatusMap[key]();
  updateInitStatusBeforeAdd(defaultComStatus, key);
  initState[key] = defaultComStatus;
});

export class MaterialStore {
  currentMaterialCom: Material = ComsTypeEnum.singleSelect;
  coms: Record<Material, Status> = {
    [ComsTypeEnum.singleSelect]: defaultStatusMap[ComsTypeEnum.singleSelect](),
    [ComsTypeEnum.muSelect]: defaultStatusMap[ComsTypeEnum.muSelect](),
    // [ComsTypeEnum.optionSelect]: defaultStatusMap[ComsTypeEnum.optionSelect](),
    [ComsTypeEnum.singlePicSelect]: defaultStatusMap[ComsTypeEnum.singlePicSelect]()
    // [ComsTypeEnum.muPicSelect]: defaultStatusMap[ComsTypeEnum.muPicSelect](),
    // [ComsTypeEnum.textNode]: defaultStatusMap[ComsTypeEnum.textNode](),
    // [ComsTypeEnum.personInfoGender]: initState[ComsTypeEnum.personInfoGender],
    // [ComsTypeEnum.personInfoEdu]: initState[ComsTypeEnum.personInfoEdu]
  };

  constructor() {
    makeAutoObservable(this);
  }

  setCurrentMaterialCom(comName: Material) {
    this.currentMaterialCom = comName;
  }
  // status为string时，设置status值
  setTextStatus = setTextStatus;
  // statsus为字符串数组时，根据下标设置指定项值
  // 如optionProps.status为[1,2]传入payload={value:2,index:0},则改为[2,2]
  setValueByIndex = setValueByIndex;
  // statsus为字符串数组时，添加选项
  addOption = addOption;
  // statsus为字符串数组时，删除选项
  rmOption = rmOption;
  // statsus为字符串数组时，设置选中项
  setCurrentStatus = setCurrentStatus;
  // statsus为图片对象数组时，设置图片链接
  // status: [{ value: '', picTitle: '图片标题1',picDesc: '图片描述1'}]）
  setPicLinkByIndex = setPicLinkByIndex;
}

export const useMaterialStore = new MaterialStore();
